package com.example.demo.config;

import com.example.demo.common.Interceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.session.data.redis.config.ConfigureRedisAction;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@EnableAsync
@EnableRedisHttpSession
public class SessionConfig implements WebMvcConfigurer {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getSessionInterceptor()).
        //全局拦截
        addPathPatterns("/**").
        //不拦截的路径
        excludePathPatterns("/error", "/user/**");
    }

    @Bean
    public Interceptor getSessionInterceptor() {
        return new Interceptor();
    }

    @Bean
    public static ConfigureRedisAction configureRedisAction() {
        //让springSession不再执行config命令
        return ConfigureRedisAction.NO_OP;
    }
//    @Configuration
//    class TaskPoolConfig {
//
//        @Bean("taskExecutor")
//        public Executor taskExecutor() {
//            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//            executor.setCorePoolSize(10);
//            executor.setMaxPoolSize(500);
//            executor.setQueueCapacity(200);
//            executor.setKeepAliveSeconds(60);
//            executor.setThreadNamePrefix("taskExecutor-");
//            executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//            return executor;
//        }
//    }
}
